<script>

    (function($){
        function crateCommentInfo(obj){

            if(typeof(obj.time) == "undefined" || obj.time == ""){
                obj.time = "未获取到！";
            }else{
                obj.time = obj.time;
                //obj.time = timestampToTime(obj.time);
                //console.log()
            }

            var el = "<div class='comment-info' id='comment"+obj.comm_id+"' name='comment"+obj.id+"' data-comm-id='"+obj.comm_id+"' data-user-id='"+obj.user_id+"'>";
            switch(obj.sex)
            {
                case 0:
                    el =el + "<header style='background-color: #4E5465'><img src='/upload/"+obj.img+"'></header><div class='comment-right'><h3>"+obj.replyName+"</h3>"
                    break;
                case 1:
                    el =el + "<header style='background-color: #59a9e9'><img src='/upload/"+obj.img+"'></header><div class='comment-right'><h3>"+obj.replyName+"</h3>"
                    break;
                case 2:
                    el =el +  "<header style='background-color: #e9590d'><img src='/upload/"+obj.img+"'></header><div class='comment-right'><h3>"+obj.replyName+"</h3>"
                    break;
                default:
                    el =el +  "<header style='background-color: #ffffff'><img src='/upload/"+obj.img+"'></header><div class='comment-right'><h3>"+obj.replyName+"</h3>"
            }

            el = el+"<div class='comment-content-header'><span><i class='glyphicon glyphicon-time'></i>"+obj.time+"</span>";

            if(typeof(obj.address) != "undefined"){
                el =el+"<span><i class='glyphicon glyphicon-map-marker'></i>"+obj.address+"</span>";
            }
            el = el+"</div><p class='content' style='margin-top: 20px; font-size: 15px'>"+obj.content+"</p><div class='comment-content-footer'><div class='row'><div class='col-md-10'>";

            if(typeof(obj.osname) != "undefined" && obj.osname != ""){
                el =el+"<span><i class='glyphicon glyphicon-pushpin'></i> 来自:"+obj.osname+"</span>";
            }

            if(typeof(obj.browse) != "undefined" && obj.browse != ""){
                el = el + "<span><i class='glyphicon glyphicon-home'></i> "+obj.browse+"</span>";
            }//来自的浏览器

            if(obj.id != ""){
                el =el+"<span class='floor-num' style='padding-right: 10px;'><i class='glyphicon glyphicon-inbox'>&nbsp;</i>"+obj.id+"</span><span>层</span>";
            }

            el = el + "</div><div class='col-md-2'><span class='reply-btn'>回复</span></div></div></div><div class='reply-list'>";
            if(obj.replyBody != "" && obj.replyBody.length > 0){
                var arr = obj.replyBody;
                for(var j=0;j<arr.length;j++){
                    var replyObj = arr[j];
                    el = el+createReplyComment(replyObj);
                }
            }
            el = el+"</div></div></div>";
            return el;
        }

        //返回每个回复体内容
        function createReplyComment(reply){
            var replyEl = "<div class='reply' id='comment"+reply.comm_id+"'><div><a href='javascript:void(0)' class='replyname'>"+reply.replyName+"</a>:<a href='javascript:void(0)'>@"+reply.beReplyName+"</a><span>"+reply.content+"</span></div>"
                + "<p><span>"+reply.time+"</span> <span class='reply-list-btn'>回复</span></p></div>";
            return replyEl;
        }
        //将时间戳转换成时间
        function timestampToTime(timestamp) {
            var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
            var D = date.getDate() + ' ';
            var h = date.getHours() + ':';
            var m = date.getMinutes() + ':';
            var s = date.getSeconds();
            return Y+M+D+h+m+s;
        }
        function getNowDateFormat(){
            var nowDate = new Date();
            var year = nowDate.getFullYear();
            var month = filterNum(nowDate.getMonth()+1);
            var day = filterNum(nowDate.getDate());
            var hours = filterNum(nowDate.getHours());
            var min = filterNum(nowDate.getMinutes());
            var seconds = filterNum(nowDate.getSeconds());
            return year+"-"+month+"-"+day+" "+hours+":"+min+":"+seconds;
        }
        function filterNum(num){
            if(num < 10){
                return "0"+num;
            }else{
                return num;
            }
        }

        function replyClick(el){
            el.parent().parent().append("<div class='replybox'><textarea cols='80' rows='50' placeholder='来说几句吧......' class='mytextarea' ></textarea><span class='send'>发送</span></div>")
                .find(".send").click(function(){
                var content = $(this).prev().val();
                var parent_id = $(this).parent().parent().parent().parent().parent().attr('data-comm-id');
                var floor = $(this).parent().parent().parent().parent().parent().attr('name').replace(/[^0-9]/ig,"");
                var full_name = "{$_SESSION.think.user.user_nickname | default=''}";
                var user_id = "{$_SESSION.think.user.id | default=''}";
                var object_id ="{$article['id']}";
                var to_user_id =$(this).parent().parent().parent().parent().parent().attr('data-user-id');
                var url =  window.location.href;
                //console.log(full_name);
                //debugger;
                if(content != ""){
                    var parentEl = $(this).parent().parent().parent().parent();
                    var obj = new Object();
                    $.post("{:url('user/Comment/add')}",{
                        content: content,
                        parent_id: parent_id,
                        floor: floor,
                        type: 1,
                        user_login: full_name,
                        url: url,
                        user_id: user_id,
                        object_id: object_id,
                        to_user_id: to_user_id},
                        function (data) {
                        //console.log(data);
                        if(data.code==1){
                            obj.replyName = "{$_SESSION.think.user.user_nickname | default='匿名'}";
                            if(el.parent().parent().hasClass("reply")){
                                //console.log("111111111111");
                                obj.beReplyName = el.parent().parent().find("a:first").text();
                            }else{
                                //console.log("222222222222222");
                                obj.beReplyName=parentEl.find("h3").text();
                            }
                            obj.content=content;
                            obj.time = getNowDateFormat();
                            var replyString = createReplyComment(obj);
                            $(".replybox").remove();
                            parentEl.find(".reply-list").append(replyString).find(".reply-list-btn:last").click(function(){
                                noty({
                                    timeout: 1500,
                                    text: "请不要试图回复自己的回复！！！",
                                    type: 'error',
                                    layout: "center",
                                    modal: true,
                                });
                            });
                        }else if(data.code == 0){
                            noty({
                                timeout: 1500,
                                text: data.msg,
                                type: 'error',
                                layout: "center",
                                modal: true,
                            });
                        }
                    });
                }else{
                    Wind.use('noty', function () {
                        noty({
                            timeout: 1000,
                            text: "没有读到内容啊！",
                            type: 'error',
                            layout: "center",
                            modal: true,
                        });
                    });
                }
            });
        }


        $.fn.addCommentList=function(options){
            var defaults = {
                data:[],
                add:""
            }
            var option = $.extend(defaults, options);
            //加载数据
            if(option.data.length > 0){
                var dataList = option.data;
                var totalString = "";
                for(var i=0;i<dataList.length;i++){
                    var obj = dataList[i];
                    var objString = crateCommentInfo(obj);
                    totalString = totalString+objString;
                }
                $(this).append(totalString).find(".reply-btn").click(function(){
                    if($(this).parent().parent().find(".replybox").length > 0){
                        $(".replybox").remove();
                    }else{
                        $(".replybox").remove();
                        replyClick($(this));
                    }
                });
                $(".reply-list-btn").click(function(){
                    if($(this).parent().parent().find(".replybox").length > 0){
                        $(".replybox").remove();
                    }else{
                        $(".replybox").remove();
                        replyClick($(this));
                    }
                })
            }

            //添加新数据
            if(option.add != ""){
                obj = option.add;
                var str = crateCommentInfo(obj);
                $(this).prepend(str).find(".reply-btn").click(function(){
                    replyClick($(this));
                });
            }
        }


    })(jQuery);
</script>